package com.xzq.socket.nio.server;

import lombok.Data;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.charset.Charset;

/**
 * @Author xzq
 * @Description //TODO
 * @Date 2021/11/18 20:05
 * @Version 1.0.0
 **/
@Data
public class NioServer {
    private Selector selector;
    private ServerSocketChannel ssc;
    private Integer port;

    public NioServer(Integer port) {
        this.port = port;
        try {
            selector = Selector.open();
            ssc= ServerSocketChannel.open();
            ssc.configureBlocking(false);
            ssc.socket().bind(new InetSocketAddress(port),1024);
            ssc.register(selector, SelectionKey.OP_ACCEPT);
            new NioServerHandler(selector, Charset.forName("gbk")).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new NioServer(9999);
    }


}
